#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
390. Elimination Game

There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.

Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.

We keep repeating the steps again, alternating left to right and right to left, until a single number remains.

Find the last number that remains starting with a list of length n.

Example:

Input:
n = 9,
1 2 3 4 5 6 7 8 9
2 4 6 8
2 6
6

Output:
6

'''
import datetime
import copy
class Solution(object):
    def lastRemaining(self, n):
        """
        :type n: int
        :rtype: int
        """
        tmp_list=list(range(1, n+1))
        exchange_list=[]
        while True:
            len_tmp_list=len(tmp_list)
            if len_tmp_list == 1:
                return tmp_list[0]
            else:
                # left -> right
                exchange_list=[]
                exchange_list = [tmp_list[i] for i in range(1, len_tmp_list, 2)]
                tmp_list=[]
                len_exchange_list=len(exchange_list)
                if len_exchange_list == 1:
                    return exchange_list[0]
                else:  # right -> left
                    mod = len_exchange_list % 2
                    tmp_list=[exchange_list[i] for i in range(mod, len_exchange_list, 2) ]

    def lastRemaining_from_kfrancischen(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = n
        start = 1
        end = n
        direction = 1
        gap = 2
        while start < end:
            if direction:
                if (end-start) % gap == 0:
                    end -= gap // 2
                start = start + gap // 2
                direction = 0
            else:
                if (end - start) % gap == 0:
                    start = start + gap // 2
                end = end - gap // 2
                direction = 1
            gap *= 2
        return start

    def lastRemaining_from_luffy_zc(self, n):
        """
        each round the remaining list can be represented as (start,count,step)
        :type n: int
        :rtype: int
        """
        start, count, step = 1, n, 1
        while count > 1:
            end = start + (count - 1) * step
            # compute the next round
            start = end - (count % 2) * step
            count //= 2
            step *= -2
        return start

    def lastRemaining_from_jedihy(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = n
        head = 1
        isFromLeft = True
        step = 1
        while count > 1:
            if isFromLeft or count % 2 == 1:
                head = head + step
            count //= 2
            step *= 2
            isFromLeft = not isFromLeft
        return head

if __name__=='__main__':
    
    solution=Solution()

    start_time=datetime.datetime.now()
    ret=solution.lastRemaining(2121)
    print(ret)
    end_time=datetime.datetime.now()
    print(end_time - start_time)
    
    start_time=datetime.datetime.now()
    ret=solution.lastRemaining(10002121)
    print(ret)
    end_time=datetime.datetime.now()
    print(end_time - start_time)

    start_time=datetime.datetime.now()
    ret=solution.lastRemaining_from_kfrancischen(10002121)
    print(ret)
    end_time=datetime.datetime.now()
    print(end_time - start_time)

    start_time=datetime.datetime.now()
    ret=solution.lastRemaining_from_luffy_zc(10002121)
    print(ret)
    end_time=datetime.datetime.now()
    print(end_time - start_time)
    
    start_time=datetime.datetime.now()
    ret=solution.lastRemaining_from_jedihy(10002121)
    print(ret)
    end_time=datetime.datetime.now()
    print(end_time - start_time)



